﻿using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateFromMySql NewDate = new DateFromMySql();
        ReviewerMySql SQL = new ReviewerMySql();
        int x = 0;
        int y = 1;
        int i = 0;
        string[] Maaned = Regex.Split("Blank,Jan,Feb,Mar,Apr,Maj,Jun,Jul,Aug,Sep,Okt,Nov,Dec", ",");
        string SQLsaetning = "";
        int CurrMonth = 0;
        int LastMonth = 0;
        int CurrYear = 0;
        string Format = Request.QueryString["Format"];
        string Formatnavn = Request.QueryString["Navn"];
        string DatoTilInt = Request.QueryString["Dato"];
        string Search = Request.QueryString["Ord"];
        int Dato = 0;
        DivMaaned.InnerHtml = "";
        foreach (string Man in Maaned)
        {
            if (Man != "Blank")
            {
                DivMaaned.InnerHtml += "<a href=\"?Dato=" + y + "\">" + Man + "</a> | ";
                y++;
            }
        }

        HyperLinkXbox.NavigateUrl = "?Navn=Xbox 360&Format=1";
        HyperLinkPC.NavigateUrl = "?Navn=PC&Format=2";
        HyperLinkPlayStation3.NavigateUrl = "?Navn=Playstation 3&Format=3";
        HyperLinkNintendoWii.NavigateUrl = "?Navn=Nintendo Wii&Format=4";


        if (Format == "" || Dato == 0 || Format == null || Dato == null)
        {
            SQLsaetning = "SELECT V2_Spil.*, V2_Spil.ID AS MainID, V2_GloFormat.Navn AS FormatNavn FROM V2_Spil JOIN V2_GloFormat ON V2_GloFormat.id = V2_Spil.Format WHERE V2_Spil.ReleaseDato <> '0000-00-00' AND V2_Spil.ReleaseDato >= CURDATE() ORDER BY V2_Spil.ReleaseDato, V2_Spil.Titel, V2_GloFormat.Navn";
            lblOverskrift.Text = "Kommende udgivelser";
        }

        if (Format != "" && Format != null)
        {
            SQLsaetning = "SELECT V2_Spil.*, V2_Spil.ID AS MainID, V2_GloFormat.Navn AS FormatNavn FROM V2_Spil JOIN V2_GloFormat ON V2_GloFormat.id = V2_Spil.Format WHERE V2_Spil.ReleaseDato <> '0000-00-00' AND V2_Spil.ReleaseDato >= CURDATE() AND V2_Spil.Format = " + Format + " ORDER BY V2_Spil.ReleaseDato, V2_Spil.Titel";
            lblOverskrift.Text = "Kommende udgivelser til " + Formatnavn;
        }

        if (DatoTilInt != "" && DatoTilInt != null)
        {
            Dato = int.Parse(DatoTilInt);
            int NextYear = 0;
            if (Dato < DateTime.Now.Month)
            {
                NextYear = DateTime.Now.AddYears(1).Year;
            }
            else
            {
                NextYear = DateTime.Now.Year;
            }
            SQLsaetning = "SELECT V2_Spil.*, V2_Spil.ID AS MainID, V2_GloFormat.Navn AS FormatNavn FROM V2_Spil JOIN V2_GloFormat ON V2_GloFormat.id = V2_Spil.Format WHERE V2_Spil.ReleaseDato <> '0000-00-00' AND V2_Spil.ReleaseDato BETWEEN '" + NextYear + "-" + Dato + "-01' AND '" + NextYear + "-" + Dato + "-31'  ORDER BY V2_Spil.ReleaseDato, V2_Spil.Titel, V2_GloFormat.Navn";
            lblOverskrift.Text = "Kommende udgivelser " + Maaned[Dato] + " " + NextYear.ToString();
        }

        if (Search != "" && Search != null)
        {
            SQLsaetning = "SELECT V2_Spil.*, V2_Spil.ID AS MainID, V2_GloFormat.Navn AS FormatNavn FROM V2_Spil JOIN V2_GloFormat ON V2_GloFormat.id = V2_Spil.Format WHERE V2_Spil.ReleaseDato <> '0000-00-00' AND V2_Spil.ReleaseDato >= CURDATE() AND V2_Spil.Titel LIKE '%" + Search + "%' ORDER BY V2_Spil.ReleaseDato";
            lblOverskrift.Text = "Søgning på :" + Search;
        }

        DivResult.InnerHtml += "<table width=\"100%\">";
        OdbcDataReader MainReader = SQL.ExecuteReader(SQLsaetning);
        while (MainReader.Read())
        {
            i++;
            CurrMonth = DateTime.Parse(NewDate.GetDate(MainReader["ReleaseDato"].ToString())).Month;
            CurrYear = DateTime.Parse(NewDate.GetDate(MainReader["ReleaseDato"].ToString())).Year;
            if (CurrMonth != LastMonth)
            {
                DivResult.InnerHtml += "<tr><td style=\"border-top:1px solid #666666; border-bottom:1px solid #666666; font-weight:bold; font-size:14px; color:#666666; padding:5px;\" colspan=\"3\" class=\"TDrowTwo\">" + Maaned[CurrMonth] + " " + CurrYear + "</td></tr>";
                DivResult.InnerHtml += "<tr><td><a href=\"" + UI.LinkMake.LinkSpil(MainReader["MainID"].ToString(), MainReader["Titel"].ToString()) + "\">" + MainReader["Titel"].ToString() + "</a></td><td>" + MainReader["FormatNavn"].ToString() + "</td><td>" + NewDate.GetDate(MainReader["ReleaseDato"].ToString()) + "</td></tr>";
                x = 0;
            }
            else
            {
                if (x == 0)
                {
                    DivResult.InnerHtml += "<tr><td class=\"TDrowTwo\"><a href=\"" + UI.LinkMake.LinkSpil(MainReader["MainID"].ToString(), MainReader["Titel"].ToString()) + "\">" + MainReader["Titel"].ToString() + "</a></td><td class=\"TDrowTwo\">" + MainReader["FormatNavn"].ToString() + "</td><td class=\"TDrowTwo\">" + NewDate.GetDate(MainReader["ReleaseDato"].ToString()) + "</td></tr>";
                    x++;
                }
                else
                {
                    DivResult.InnerHtml += String.Format("<tr><td><a href=\"{0}\">{1}</a></td><td>{2}</td><td>{3}</td></tr>"
                        , UI.LinkMake.LinkSpil(MainReader["MainID"].ToString(), MainReader["Titel"].ToString()) //Link
                        , MainReader["Titel"].ToString() //Titel
                        , MainReader["FormatNavn"].ToString() //Format
                        , NewDate.GetDate(MainReader["ReleaseDato"].ToString()));
                    
                    //<a href=\"" + UI.LinkMake.LinkSpil(MainReader["MainID"].ToString(), MainReader["Titel"].ToString()) + "\">" + MainReader["Titel"].ToString() + "</a></td><td>" + MainReader["FormatNavn"].ToString() + "</td><td>" + NewDate.GetDate(MainReader["ReleaseDato"].ToString()) + "</td></tr>";
                    x = 0;
                }
            }
            LastMonth = CurrMonth;
        }
        DivResult.InnerHtml += "</table>";

        if (i == 0)
        {
            DivResult.InnerHtml = "<center><i>Der er pt. ingen udgivelser i denne måned</i></center>";
        }
        //HtmlMeta HeaderDescription = new HtmlMeta();
        //HeaderDescription.Name = "description";
        //HeaderDescription.Content = "Releaseliste. Udgivelsesdato over kommende spil til PlayStation 3, Xbox 360, Wii og PC";
        //this.Header.Controls.Add(HeaderDescription);
    }
}
